package com.lzb.newloan;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import com.lzb.newloan.utils.BaseActivity;
import com.lzb.newloan.bqs.CatLoadingView;

public class WebviewActivity extends BaseActivity {

	// private String url="https://zmopenapi.zmxy.com.cn/openapi.do?" +
	// "sign=SAnPPlzF4qTzEa1%2BpjBcXtSA1Fk2k6cUMjkdLTaSQYkalFtksRWOiA8Qc5xYgFFb%2FkZRUIm2YJu1xq4M7pMJ5Sgl7XZPGgtKF2IgEFgEOToooikNeG0ys6jrb2MtDStFDJg8AgVmauLIW36vrddjT2weBVNMNJxWT3zctxj54Ks%3D&platform="
	// +
	// "zmop&sign_type=RSA&charset=UTF-8&app_id=1002180&method=zhima.auth.info.authorize&channel=apppc&version=1.0&"
	// +
	// "params=pVIJ1s35o87KaUO%2BCZyQHZOirOErGD4%2B8WPN3QjPOR8thT4ChCT5KutKGKRZ6KPpxejDnRwdQRI4jMSNztrQJgTnlLTcZjOau0qyirMXxOzGPZHT0%2FqNrctVHrN2FsQnfd0ejP6FTQkLual%2BpMjKjqshEInZqdIjHNxXe4lWsGHWSF%2F19KFkCUFfSTwSx"
	// +
	// "f1LSQZfS47sFKB76bqpaYEvrSCzdvKNtCZQXdIQFFocRl5uuoLju6YpDuIcZd7I0q0yyEX6GZreP0K%2B5xGb%2BplzfCPwcjjVIh%2BerhCmW58dL9jEZeRrrHhai0bHSMkC05tivb4I%2BKY%2FtCF5uvMP%2Fj1mnlGVoFr57nEqa%2Fn4JySTJgkk2xyKvP8%2BVJWpC1lcWgsqZ8NhmU7ZxkFaADCu2Q9s%2Fx6WyaEkunwx73YX4qnxAxrObDH52OR2Rh%2B4PYcy%2FX9Pf1zpIfIQnFB7fr2hj1MSvb2mS9d2qIvHnazd45OZa9eoTH7jzrvq9E%2BXuRMmxJSk";
	private String url = "";
	private WebSettings webSettings;
	private WebView home_webview;
	private CatLoadingView dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);
		initView();
	}

	private void initView() {
		dialog = new CatLoadingView(WebviewActivity.this);
		dialog.show();
		url = getIntent().getStringExtra("url");
		RelativeLayout backpress = (RelativeLayout) findViewById(R.id.backpress);
		backpress.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(new Intent(WebviewActivity.this,
						MyInfoActivity.class));
			}
		});

		home_webview = (WebView) findViewById(R.id.webview);
		webSettings = home_webview.getSettings();
		home_webview.setWebViewClient(new WebViewClient() {

			@Override
			public void onPageFinished(WebView view, String url) {
				if (!webSettings.getLoadsImagesAutomatically()) {
					webSettings.setLoadsImagesAutomatically(true);
				}
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				dialog.show();
				if(url.contains("https://api.baiqishi.com/credit/zhima/callback?params=")){
					Intent intent = new Intent(WebviewActivity.this,
							IndexActivity.class);
					intent.putExtra("id",1);
					startActivity(intent);
					view.loadUrl(url);// 载入网页
					return true;
				}

				return false;
			}
		});

		// 设置WebView属性，能够执行Javascript脚本
		webSettings.setJavaScriptEnabled(true);
		// 设置可以访问文件
		webSettings.setAllowFileAccess(true);
		// 设置支持缩放
		webSettings.setBuiltInZoomControls(false);
		if (Build.VERSION.SDK_INT >= 19) {
			webSettings.setLoadsImagesAutomatically(true);
		} else {
			webSettings.setLoadsImagesAutomatically(false);
		}
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			// home_webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		}
		home_webview.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				if (newProgress == 100) {
					dialog.dismiss();// 如果全部载入,隐藏进度对话框
				}


				super.onProgressChanged(view, newProgress);
			}



		});
		home_webview.loadUrl(url);
	}

	@Override
	protected void onDestroy() {
		// setConfigCallback(null);
		super.onDestroy();
		home_webview.removeAllViews();
		home_webview.destroy();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Log.d("tag", "DemoActivity.onActivityResult");
		// onActivityResult callback
//		CreditApp.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void processMessage(Message message) {
		// TODO Auto-generated method stub

	}

}
